<?php
class IndexControl extends CategoryModule
{
	public $layout = false;

	private $_e = array(
		1 => '该分类下还有子分类未删除',
	);

	/**
	 * 栏目管理
	 */
	public function actionCategory()
	{
		D::import('module:model.model.modelModel');
		$var = array(
			'tree' => categoryModel::model()->getTree(
				$this->site_id,
				array(
					'　　│&nbsp;',
					'　　├&nbsp;',
					'　　└&nbsp;'
				)
			),
			'model' => modelModel::model()->getModelName(),
		);
		$this->render('category', $var);
	}
	
	/**
	 * 添加栏目
	 */
	public function actionCategoryAdd($type = 0, $pid = 0, $mid = 0)
	{
		$encrypt_key  = D::$config['encrypt'];
		D::import('module:model.model.modelModel');
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_addcategory') != 'add_category') {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_1']) );
			} else {
				//检测是否有在上级栏目添加子栏目的权限，如果是顶级则跳过
				if ( $type != 2) {
					$this->_checkAuth($_POST['parent_id'], 'add');
				}
				$_POST['site_id'] = $this->site_id;
				$pid    = $_POST['parent_id'];
				//列表
				if ($type == 0) {
					$cat_id = categoryModel::model()->typeOne()->insert();
					$url    = D::createUrl(
						'category/index/categoryadd',
						array('type'=>0, 'pid'=>$pid, 'mid'=>$mid)
					);
				//单页
				} else if ($type == 1) {
					$cat_id = categoryModel::model()->typeOne()->insert();
					$url    = D::createUrl('category/index/pageadd', array('pid' => $pid));
				//跳转
				} else if ($type == 2) {
					$cat_id = categoryModel::model()->typeTwo()->insert();
					$url    = D::createUrl('category/index/jumpadd', array('pid' => $pid));
				}

				//跳转栏目不进行权限设置
				if ($type == 0 || $type == 1) {
					categoryAuthModel::model()->saveCategoryRoleAuth($cat_id);
				}

				$links = array(
					$url,
					'继续添加',
					3,			//菜单ID
					'返回列表',
				);
				$this->ajaxReturn( $this->ajaxGoto($this->msg['yes_3'], $links) );
			}
		}

		//模板推送变量
		$var  = array(
			'tree_data' => categoryModel::model()->getHtmlTree($this->site_id, $pid),
			'model_data'=> modelModel::model()->getModelHtmlOption($mid),
			'roles'		=> roleModel::model()->getRoleName(),
			'token_code' => $this->encrypt->encode('add_category', $encrypt_key.'_addcategory'),
		);

		//根据不同的栏目类型，调用不同的模板
		switch ($type) {
			case 1 :
				$template = 'category_page_add';
				break;
			case 2 :
				$template = 'category_jump_add';
				break;
			default :
				$template = 'category_add';
		}
		$this->render($template, $var);
	}

	public function actionCategoryAddMenu($url = '')
	{
		$this->ajaxReturn($this->getSaveButton($url, 'add_category'), 'text');
	}

	/**
	 * 添加单页
	 */
	public function actionPageAdd($pid = 0)
	{
		$this->actionCategoryAdd(1, $pid);
	}

	public function actionPageAddMenu($url = '')
	{
		$this->ajaxReturn($this->getSaveButton($url, 'add_category'), 'text');
	}

	/**
	 * 添加跳转栏目
	 */
	public function actionJumpAdd($pid = 0)
	{
		$this->actionCategoryAdd(2, $pid);
	}

	public function actionJumpAddMenu($url = '')
	{
		$this->ajaxReturn($this->getSaveButton($url, 'add_category'), 'text');
	}

	/**
	 * 修改栏目
	 */
	public function actionCategoryEdit($type = 0, $cat_id = 0, $mid = 0)
	{
		$encrypt_key  = D::$config['encrypt'];
		D::import('module:model.model.modelModel');
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_addcategory') != 'add_category') {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_1']) );
			} else {
				//检测是否有在上级栏目添加子栏目的权限，如果是顶级则跳过
				if ($type != 2) {
					$this->_checkAuth($_POST['parent_id'], 'edit');
				}
				$_POST['site_id'] = $this->site_id;
				//列表，单页
				if ($_POST['type'] == 0 || $_POST['type'] == 1) {
					categoryModel::model()->typeOne()->updateByPk((int)$_POST['cat_id']);
				//跳转
				} else if ($_POST['type'] == 2) {
					categoryModel::model()->typeTwo()->updateByPk((int)$_POST['cat_id']);
				}

				//跳转栏目不进行权限设置
				if ($_POST['type'] == 0 || $_POST['type'] == 1) {
					categoryAuthModel::model()->updateCategoryRoleAuth((int)$_POST['cat_id']);
				}

				$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_4'], D::createUrl('category/index/category')) );
			}
		}

		$cat = categoryModel::model()->findByPk($cat_id);

		//获取栏目权限
		if ($type == 0 || $type == 1) {
			categoryAuthModel::model()->getCategoryAuth($cat, $cat_id);
		}

		//模板推送变量
		$var  = array(
			'tree_data' => categoryModel::model()->getHtmlTree($this->site_id, $cat['parent_id']),
			'model_data'=> modelModel::model()->getModelHtmlOption($mid),
			'roles'		=> roleModel::model()->getRoleName(),
			'token_code' => $this->encrypt->encode('add_category', $encrypt_key.'_addcategory'),
			'cat'		=> $cat,
			'stauts'	=> 'edit',
		);

		//根据不同的栏目类型，调用不同的模板
		switch ($type) {
			case 1 :
				$template = 'category_page_add';
				break;
			case 2 :
				$template = 'category_jump_add';
				break;
			default :
				$template = 'category_add';
		}
		$this->render($template, $var);
	}

	public function actionCategoryEditMenu($url = '')
	{
		$this->ajaxReturn($this->getSaveButton($url, 'add_category'), 'text');
	}

	/**
	 * 分类排序
	 */
	public function actionEditSort($cat_id = 0, $value = 0)
	{
		$cat_id = (int)$cat_id;
		$value  = (int)$value;
		$cat    = categoryModel::model()->findByPk($cat_id);
		if ($cat['type'] != 2) {
			$this->_checkAuth($cat_id, 'sort');
		}
		if (empty($cat_id)) {
			$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_2']) );
		} else {
			categoryModel::model()->updateByPk($cat_id, array('sort'=>$value));
			$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_1']) );
		}
	}

	/**
	 * 分类删除
	 */
	public function actionCategoryDelete($cat_id = 0)
	{
		$cat_id = (int)$cat_id;
		$cat    = categoryModel::model()->findByPk($cat_id);
		if ($cat['type'] != 2) {
			$this->_checkAuth($cat_id, 'delete');
		}
		if (empty($cat_id)) {
			$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_2']) );
		} else {
			$child = categoryModel::model()->find('parent_id='.$cat_id);
			if (!empty($child)) {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->_e[1]) );
			} else {
				$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_2']) );
			}
		}
	}

	/**
	 * 更改显示状态
	 */
	public function actionChangeShow($cat_id = 0, $show)
	{
		$cat_id = (int)$cat_id;
		if (empty($cat_id)) {
			$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_2']) );
		} else {
			categoryModel::model()->updateByPk($cat_id, array('is_show'=>(int)$show));
			$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_1']) );
		}
	}

	/**
	 * 检测管理员对栏目是否有操作权限
	 * @param int    $cat_id 上级分类ID
	 * @param string $action 动作名
	 * @return boolean
	 */
	private function _checkAuth($cat_id, $action)
	{
		$info = array(
			'add'    => '你没有权限在此栏目下增加新的栏目',
			'edit'   => '你没有权限编辑此栏目',
			'delete' => '你没有权限删除此栏目',
			'sort'   => '你没有权限对此栏目进行排序',
			'move'   => '你没有权限对此栏目的数据进行移动',
			'look'   => '你没有权限查看此栏目的信息和数据',
		);
		if ($cat_id == 0) return true;
		$roles  = D::app()->auth()->getUserRole(D::app()->user()->getId(), true);
		if (!categoryAuthModel::model()->getAuth($cat_id, $roles[0]['rid'], $action)) {
			$this->ajaxReturn( $this->ajaxInfo(0, $info[$action]) );
		}
	}

}
?>